package hangman;

import java.util.ArrayList;
import java.util.Collection;

public class HangmanGame {

    private boolean isOver;
    private ArrayList<Character> originalWord;
    private ArrayList<Character> guessedWord;
    private ArrayList<Character> charactersGuessed;
    private ArrayList<Character> charactersLeft;
    private int turn = 0;

    public HangmanGame( ) {

    	String word 	   = getRandomWord(); 
        isOver             = false;
        originalWord       = (ArrayList<Character>)getWordContainerFromString(word);
        guessedWord        = (ArrayList<Character>)createEmptyContainer(word.length());
        charactersGuessed  = new ArrayList<Character>(word.length());
        charactersLeft     = getAllCharacters();
        
    }

    public String start(){
        return "\n";
    }

    public ArrayList<Character> getCharactersSelected(){
    	return charactersGuessed;
    }

    public ArrayList<Character> getCharactersLeft(){
    	return charactersLeft;
    }
    
    public int getTurn(){
    	return turn;
    }
    
    public ArrayList<Character> getGuessedWord(){
    	return guessedWord;
    }
      
    
	private String getStringFromWordContainer( Collection<Character> container ) {

        StringBuilder word = new StringBuilder("");

        for( Character c : container ){
            word.append(c.toString());
        }

        return word.toString();
    }

    private Collection<Character> getWordContainerFromString(String word) {

        ArrayList<Character> container = new ArrayList<Character>();

        for( int i = 0; i < word.length(); i++ ){
            container.add(word.charAt(i));
        }

        return container;
    }

    private Collection<Character> createEmptyContainer( int size ){

        ArrayList<Character> container = new ArrayList<Character>();

        for(int i = 0; i< size; i++){
            container.add( ' ' );
        }

        return container;
    }

    public void guess( Character character ){	

    	charactersLeft.remove(character);
    	
        ArrayList<Integer> indexes = new ArrayList<Integer>();

        for(int i = 0; i < originalWord.size(); i++ ){
            if( originalWord.get(i) == character ){
                indexes.add(i);
            }
        }
        
        if( ! indexes.isEmpty() ){

            for( Integer index : indexes ){             
                guessedWord.set( index, character );
            }
            
        }else{
        	
        	if( ! charactersGuessed.contains( character ) ){
        		charactersGuessed.add(character);
      		}        	
        	
            turn++;
        }

        if(turn == 6){
            isOver = true;
        }

        if(containerIsComplete(guessedWord)){
            isOver = true;
        }
    }

    public boolean isOver(){
        return this.isOver;
    }

    public boolean containerIsComplete( Collection<Character> container ){
        return !container.contains( ' ' );
    }

    public String getStatus(){

        if( turn == 6){
            return "You lose";
        }

        return "You win";
    }
    
    private String getRandomWord(){
    	String word = "";
    	
    	String words [] = {
    		"inception",
    		""
    	}; 
    	
    	return "justu";
    }
    
    public ArrayList<Character> getAllCharacters(){
    	
    	ArrayList<Character> characters = new ArrayList<Character>();
    	
    	for( char i = 97; i <= 122; i++)
    		characters.add( i );
    	
    	return characters;
    }
}
